var cusUnique;//用户唯一性标识
var cusFlag;//客户身份 0-注册用户  1-游客

var imgUrl="http://121.42.189.191:80/";//全局变量

$(document).ready(function() {
	cusUnique=getCookie("cusUnique");
	if(cusUnique==null){
		window.location.href="index.html";//获取不到cusUnique的页面一律跳转到商城主页
	}
	loadSaleList(cusUnique,0);//加载用户订单信息
	findSaleStateTotals(cusUnique);//加载各订单数量
});

//根据订单状态加载订单信息
function loadSalListState(saleList){
	$(saleList).parents("ul.saleListUl").find("li").removeClass("cur");
	$(saleList).parent().addClass("cur");
	var state=$(saleList).find("span").first().text();
	loadSaleList(cusUnique,state);//加载用户订单信息
	
	
}

//加载不同状态的订单对应的数量
function findSaleStateTotals(cusUnique){
	
	$.ajax({
		url : "../online/findSaleStateTotals.do",
		type : "post",
		data : {"cusUnique":cusUnique},
		dataType : "json",
		success : function(result) {
			
			console.log(result);
			if (result.status == 1) {// 查询成功
				$("#stateAll").text(result.data[0]);
				$("#state5").text(result.data[4]);
				$("#state2").text(result.data[1]);
				$("#state3").text(result.data[2]);
				$("#state4").text(result.data[3]);

			} else {
				$("#stateAll").text(0);
				$("#state5").text(0);
				$("#state2").text(0);
				$("#state3").text(0);
				$("#state4").text(0);
				
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {

		}
	});
}



// 加载用户订单信息
function loadSaleList(cusUnique,state) {
	var totals;//查询的总条数
	$(".user_con").css("display","none");//隐藏其他的信息
	$("#personalSales").show();//显示用户的基本信息
	console.log("cusUnique:"+cusUnique);
	$.ajax({
		url : "../online/findSaleByCusId.do",
		type : "post",
		data : {"cusUnique":cusUnique,"state":state},
		dataType : "json",
		success : function(result) {
			console.log(result);
			$("#saleList").empty();
			if (result.status == 1) {// 查询成功
				$("#saleList").empty();
				var data=result.data;
				totals=result.totals;
				var tbody="";
				for (var i = 0; i < data.length; i++) {
					var tr = '	<div class="order-list clearfloat saleDiv" >'
							+ '<p class="ordernum box-s">' + '订单  <span class="saleListUnique">'
							+ data[i].saleListUnique + '</span>' + '<span>'
							+ data[i].saleListHandlestate + '</span><span class="saleListState" style="display:none">'+data[i].sale_list_handlestate2+'</span><span onclick="det(this)">详情</span><span onclick="deleteSale(this)">删除</span>' + '</p>'
							+'<div class="saleListDetail"></div>'
							+ '<a class="gopay-btn fr saleListA" onclick="saleAction(this)">' + '立即支付'
							+ '</a><a class="gopay-btn fr">￥'+data[i].saleListTotal
							+ '</a>' + '</div>';
					tr += "";
					tbody += tr;
				}
				$("#saleList").append(tbody);
				findSaleList();

			} else {
				$("#saleList").empty();
				
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {

		}
	});
	
}


//遍历订单修改订单的操作
function findSaleList(){
	$("#saleList").find("div.order-list").each(function() {
		var saleListState=$(this).find("span.saleListState").text();
		var saleListA=$(this).find("a.saleListA");
		
		if(saleListState==2||saleListState==3){
			saleListA.html("确认收货");
		}else if(saleListState==5){
			saleListA.html("立即支付");
		}else if(saleListState==4){
			saleListA.html("已完成");
		}
		
	});
}


//根据订单的状态去处理订单
function saleAction(saleState){
	
	var saleListUnique=$(saleState).parents("div.saleDiv").find("span.saleListUnique").text();
	var saleListState=$(saleState).parents("div.saleDiv").find("span.saleListState").text();
	if(saleListState==3){
		$.ajax({
			url : "../online/saleConfirm.do",
			type : "post",
			data : {"saleListUnique":saleListUnique,"state":4},
			dataType : "json",
			success : function(result) {
				if (result.status == 1) {// 查询成功
					
				} else {
					
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {

			}
		});
		
		
	}
	
}





//显示订单详情
function det(det){
	var saleListUnique=$(det).parents("div.saleDiv").find("span.saleListUnique").text();
	console.log(saleListUnique);
	$.ajax({
		url : "../online/findDetailPer.do",
		type : "post",
		data : {"saleListUnique":saleListUnique},
		dataType : "json",
		success : function(result) {
			if (result.status == 1) {// 查询成功
				$(det).parents("div.saleDiv").find("div.saleListDetail").empty();
				var data=result.data;
				console.log(result.data);
				var tbody="";
				for( var i=0;i<data.length;i++){
					var tr = '<div class="list clearfloat fl box-s">'
						+ '<a href="detail.html">' + '<div class="tu fl clearfloat">'
						+ '<img src="'+imgUrl+data[i].goods_picturepath+'"/>' + '</div>'
						+ '<div class="middle clearfloat fl">'
						+ '<p class="tit">'+data[i].goodsName+'</p>' 
						+ '<p class="price clearfloat">'
						+ '<span class="xprice fl">¥</span><span class="xprice fl">'+data[i].saleListDetailPrice+'</span>'
						+ '<span class="shu">'+data[i].saleListDetailCount+'</span><span class="shu">×</span>' + '</p>' + '</div>' + '</a>'
						+ '</div>';
					tr+="";
					tbody+=tr;
				}
				$(det).parents("div.saleDiv").find("div.saleListDetail").append(tbody);
			} else {
				alert("请重新");
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {

		}
	});

		
//		$(det).parents("div.saleDiv").find("div.saleListDetail").append(tr);
	
}


//删除订单
function deleteSale(det){
	var saleListUnique=$(det).parents("div.saleDiv").find("span.saleListUnique").text();
	$.ajax({
		url : "../online/deleteSale.do",
		type : "post",
		data : {"saleListUnique":saleListUnique},
		dataType : "json",
		success : function(result) {
			
			if (result.status == 1) {// 查询成功
				$(det).parents("div.saleDiv").remove();
				findSaleStateTotals(cusUnique);//加载各订单数量
				
			} else {
				
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
		}
	});
	
	
}















